const axios_instance = axios.create({
    baseURL: baseUrl + "mobile",
    timeout: 30000,
    headers: {'X-Requested-With':'XMLHttpRequest'},
    withCredentials: true,
});

async function postJson(url,data,error) {
    return  new Promise((resolve, reject) => {
        axios_instance.post(url, JSON.stringify(data), {
            headers: { 'Content-Type': 'application/json;charset=UTF-8' }
        }).then((result) => {
            const data = result.data;
            if (data.code === 1) {
                if (typeof error === 'function') {
                    error(data);
                    return false;
                } else {
                    vant.showToast(data.msg);
                    return false;
                }
            } else if (data.code === 401) {
                location.href = '/mobile/login/login';
            }
            return resolve(data);
        });
    });
}

async function postFile(url,data,error) {
    return  new Promise((resolve, reject) => {
        axios_instance.post(url, data, {
            headers: { 'Content-Type': 'application/form-data' }
        }).then((result) => {
            const data = result.data;
            if (data.code === 1) {
                if (typeof error === 'function') {
                    error(data);
                    return false;
                } else {
                    vant.showToast(data.msg);
                    return false;
                }
            } else if (data.code === 401) {
                location.href = '/mobile/login/login';
            }
            return resolve(data);
        });
    });
}